export const cloneOf = (obj) => {
    return JSON.parse(JSON.stringify(obj));
}

export const asyncImageLoader = (url) => {
    return new Promise((resolve, reject) => {
        var image     = new Image();
        image.src     = url;
        image.onload  = () => resolve(image);
        image.onerror = () => reject(new Error('could not load image'));
    });
};

export const autoSize = (imageSize, containerSize) => {
    let { width, height } = imageSize;

    if (!(width < containerSize.width && height < containerSize.height)) {
        if (containerSize.width / containerSize.height <= width / height) //原图片宽高比例 大于 图片框宽高比例
        {
            width  = (containerSize.width * 0.8);   //以框的宽度为标准
            height = (containerSize.width * 0.8) * (height / width);
        } else {   //原图片宽高比例 小于 图片框宽高比例
            width  = (containerSize.height * 0.8) * (width / height);
            height = containerSize.height * 0.8;   //以框的高度为标准
        }
    }
    return {
        width,
        height
    }
};


export const damping = (value, max) => {
    let y = Math.abs(value);
    y     = 0.82231 * max / (1 + 4338.47 / Math.pow(y, 1.14791));
    return Math.round(value < 0 ? -y : y);
}

export const rectBoxLimit = (pos, rectSize, clientSize, offsetValue) => {
    let { x, y }                            = pos;
    let { containerWidth, containerHeight } = clientSize;
    let { currentWidth, currentHeight }     = rectSize;
    let { currentOffsetX, currentOffsetY }  = offsetValue;
    let differenceX                         = 0;
    let differenceY                         = 0;

    let clientLimitX = containerWidth - currentOffsetX;
    if (currentWidth > containerWidth) {
        let maxX = currentOffsetX + (containerWidth * 0.3);
        let minX = clientLimitX - (containerWidth * 0.3);
        if (x > maxX) {
            differenceX = x - maxX;
            x           = maxX;
        }
        if (x < minX) {
            differenceX = x - minX;
            x           = minX;
        }
    } else {
        if (x < currentOffsetX) {
            differenceX = x - currentOffsetX;
            x           = currentOffsetX;
        }
        if (x > clientLimitX) {
            differenceX = x - clientLimitX;
            x           = clientLimitX;
        }

    }

    let clientLimitY = containerHeight - currentOffsetY;
    if (currentHeight > containerHeight) {
        let maxY = currentOffsetY + (containerHeight * 0.3);
        let minY = clientLimitY - (containerHeight * 0.3);
        if (y > maxY) {
            differenceY = y - maxY;
            y           = maxY;
        }
        if (y < minY) {
            differenceY = y - minY;
            y           = minY;
        }
    } else {
        // console.log('y,currentOffsetY :', y,currentOffsetY);
        if (y < currentOffsetY) {
            differenceY = y - currentOffsetY;
            y           = currentOffsetY;
        }
        if (y > clientLimitY) {
            differenceY = y - clientLimitY;
            y           = clientLimitY;
        }
    }

    return {
        x ,
        y,
        differenceY,
        differenceX
    };
}
